package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.LogUtils;
import com.wqtz.chechai.utils.OkHttpUtils;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.widget.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 :  Kisum
 * 创建时间: 2017/1/16
 */

public class SmsVerificationLoginActivity extends BaseActivity {

    private OkHttpUtils mOkHttpUtils = OkHttpUtils.getInstance();
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_area_number)
    TextView mTvAreaNumber;
    @BindView(R.id.ct_phonenumber)
    ClearEditText mCtPhonenumber;
    @BindView(R.id.btn_get_verification)
    Button mBtnGetVerification;
    private String mPhoneNumber;



    @Override
    public void init() {
        //绑定ButterKnife
        ButterKnife.bind(this);
    }

    @Override
    public void loadData() {

    }

    @Override
    public int setContentViews() {
        return R.layout.activity_sms_login;
    }

    @Override
    public BasePresent createPresenter() {
        return null;
    }


    /**
     * 所在地不可点，目前只支持大陆
     * @return
     */
 /*   public String getAreaNumber() {
        if (StringUtils.isEmpty(mEtAreaNumber.getText().toString())) {
            return null;
        }
        return mEtAreaNumber.getText().toString();
    }*/

    /**
     * 获取手机号码
     *
     * @return
     */
    public String getPhoneNumber() {
        if (StringUtils.isEmpty(mCtPhonenumber.getText().toString().trim())) {
            return null;
        }
        return mCtPhonenumber.getText().toString().trim();
    }

    /**
     * 返回和获取验证码按钮点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.btn_get_verification})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.iv_back:
                this.finish();
                break;
            //获取验证码
            case R.id.btn_get_verification:
                //获取手机号码
                mPhoneNumber = getPhoneNumber();
                //判断手机号码
                if (StringUtils.isEmpty(mPhoneNumber)) {
                    ToastUtils.showShort(this, "手机号不能为空！");
                } else if (!StringUtils.isValidCellphone(mPhoneNumber)) {
                    ToastUtils.showShort(this, "手机号格式不正确！");
                } else {
                    LogUtils.e("输入了正确的手机号:" + mPhoneNumber);
                    //向服务器发送验证码
                    requestVerifyCode(mPhoneNumber);
                }
                break;
        }
    }

    //获取验证码信息，进行验证码请求
    private void requestVerifyCode(final String phoneNumber) {
        //异步发送网络请求
        new AsyncTask<String,Void,Boolean>(){

            @Override
            protected Boolean doInBackground(String... params) {
                //ttl后面的值是短信有效时间(分钟)
                String json = "{\"mobilePhoneNumber\":\"{0}\",\"ttl\":10,\"name\":\"注册登陆\"}";
                json = json.replace("{0}", phoneNumber);
                return mOkHttpUtils.postJson("https://api.leancloud.cn/1.1/requestSmsCode", json);

            }

            @Override
            protected void onPostExecute(Boolean s) {
                super.onPostExecute(s);
                if (s) {
                    Toast.makeText(SmsVerificationLoginActivity.this,"验证码发送成功！",Toast.LENGTH_SHORT).show();
                    //跳转到设置密码页面
                    Intent intent = new Intent(SmsVerificationLoginActivity.this,SmsSetPasswordActivity.class);
                    intent.putExtra("phoneNumber",phoneNumber);
                    startActivity(intent);
                    SmsVerificationLoginActivity.this.finish();
                } else Toast.makeText(SmsVerificationLoginActivity.this, "验证码发送失败！",Toast.LENGTH_SHORT).show();
            }
        }.execute(phoneNumber);

    }

    @Override
    protected void onResume() {
        super.onResume();
        activitys.add(this); //添加当前的activity
    }
}
